//
//  PPMessageTimelineTableViewCell.m
//  pickpic
//
//  Created by Jinxiao on 10/6/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPMessageTimelineTableViewCell.h"
#import "PPProfileViewController.h"
#import "DGCoreTextView.h"

#define insets UIEdgeInsetsMake(10, 10, 10, 10)
#define horizontalSpacingOfAvatarAndContent 10
#define horizontalSpacingOfContentAndPicture 40
#define verticalSpacing 5
#define nameFont [UIFont systemFontOfSize:14]
#define contentFont [UIFont systemFontOfSize:14]
#define dateFont [UIFont systemFontOfSize:10]
#define avatarSize CGSizeMake(36, 36)
#define imageSize CGSizeMake(36, 36)

@interface PPMessageTimelineTableViewCell ()
@property (readwrite, nonatomic, strong) UIImageView *avatarView;
@property (readwrite, nonatomic, strong) UIImageView *pictureView;
@property (readwrite, nonatomic, strong) UILabel *nameLabel;
@property (readwrite, nonatomic, strong) UILabel *dateLabel;
@property (readwrite, nonatomic, strong) DGCoreTextView *textView;
@end

@implementation PPMessageTimelineTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    _avatarView = [[UIImageView alloc] init];
    _avatarView.layer.cornerRadius = 18;
    _avatarView.layer.masksToBounds = YES;
    _avatarView.contentMode = UIViewContentModeScaleAspectFill;
    _avatarView.clipsToBounds = YES;
    __weak typeof(self)weakSelf = self;
    [_avatarView setActionBlock:^(UIControl *control) {
        [weakSelf portraitDidPress];
    } forEvent:UIControlEventTouchUpInside];
    [self addSubview:_avatarView];

    _pictureView = [[UIImageView alloc] init];
    _pictureView.contentMode = UIViewContentModeScaleAspectFill;
    _pictureView.clipsToBounds = YES;
    [self addSubview:_pictureView];

    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = nameFont;
    [_nameLabel setActionBlock:^(UIControl *control) {
        [weakSelf portraitDidPress];
    } forEvent:UIControlEventTouchUpInside];
    _nameLabel.textColor = [UIColor colorWithRGBHex:0x2c3c78];
    [self addSubview:_nameLabel];

    _dateLabel = [[UILabel alloc] init];
    _dateLabel.font = dateFont;
    _dateLabel.textColor = [UIColor colorWithRGBHex:0x999999];
    [self addSubview:_dateLabel];

    _textView = [[DGCoreTextView alloc] init];
    _textView.backgroundColor = [UIColor clearColor];
    [self addSubview:_textView];

    return self;
}

- (void)portraitDidPress {
    PPProfileViewController *vc = [[PPProfileViewController alloc] init];
    vc.userID = _message.user.userID;
    [_delegateViewController.navigationController pushViewController:vc animated:YES];
}

- (void)setMessage:(PPTimelineMessageObject *)message {
    _message = message;

    _dateLabel.text = [message.date timeAgoForTimeline];

    _textView.editor = message.editor;

    PPTimelineUserObject *user = message.user;

    _nameLabel.text = user.name;

    [user.avatarURLString applyToImageView:_avatarView placeholder:[UIImage imageNamed:@"icon-person-70"]];

    if(message.picture != nil) {
        [message.picture.URLString applyToImageView:_pictureView placeholder:[UIImage imageNamed:@"album-nothing"]];
    } else {
        [message.album.existingCoverURLString applyToImageView:_pictureView placeholder:[UIImage imageNamed:@"album-nothing"]];
    }

    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _avatarView.frame = CGRectMake(10, 10, avatarSize.width, avatarSize.height);

    _pictureView.frame = CGRectMake(self.width - insets.right - imageSize.width, insets.top, imageSize.width, imageSize.height);

    [_dateLabel sizeToFit];
    CGSize dateSize = _dateLabel.bounds.size;

    CGFloat contentWidth = self.width - insets.left - insets.right - avatarSize.width - imageSize.width - horizontalSpacingOfAvatarAndContent - horizontalSpacingOfContentAndPicture;

    CGFloat nameWidth = contentWidth - dateSize.width - 11;
    [_nameLabel sizeToFit];
    _nameLabel.width = MIN(nameWidth, _nameLabel.width);
    _nameLabel.top = 11;
    _nameLabel.left = _avatarView.right + 10;

    _dateLabel.left = _nameLabel.right + 11;
    _dateLabel.centerY = _nameLabel.centerY;

    CGSize textSize = [_message.editor sizeThatFits:CGSizeMake(contentWidth, self.height)];
    _textView.frame = CGRectMake(_nameLabel.left, _nameLabel.bottom + verticalSpacing, textSize.width, textSize.height);
}

+ (CGFloat)heightForMessage:(PPTimelineMessageObject *)message constrainedSize:(CGSize)constrainedSize {
    CGFloat height = insets.top + insets.bottom;

    height += nameFont.lineHeight;

    height += verticalSpacing;

    CGSize contentSize = CGSizeMake(constrainedSize.width - insets.left - insets.right - avatarSize.width - horizontalSpacingOfAvatarAndContent - imageSize.width - horizontalSpacingOfContentAndPicture, constrainedSize.height);
    height += [message.editor sizeThatFits:contentSize].height;

    return MAX(height, insets.top + insets.bottom + avatarSize.height);
}

@end
